package com.ellecity06.cwhiskymall.ui.live

import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.cwhiskymall.bean.response.LiveRoomGoodResponse
import com.ellecity06.cwhiskymall.bean.response.LiveRoomResponse
import com.ellecity06.cwhiskymall.http.request
import com.ellecity06.cwhiskymall.ui.live.player.LivePlayerIntent
import com.ellecity06.cwhiskymall.ui.live.player.LivePlayerState

/**
 * @author ellecity06
 * @time 2024/10/22 14:49
 * @des
 */
class LiveViewModel : BaseMviViewModel<LivePlayerState, LivePlayerIntent>() {
    var currentLiveItem: LiveRoomResponse? = null
        private set
    var good: LiveRoomGoodResponse? = null
        private set

    override suspend fun collectIntent(it: LivePlayerIntent) {
        when (it) {
            LivePlayerIntent.FetchLiveRoomList -> {
                fetchLiveRoomList()
            }

            LivePlayerIntent.ExitRoom -> {
                currentLiveItem?.let {
                    exitRoom()
                }
            }

            is LivePlayerIntent.FetchLiveGood -> {
                fetchLiveRoomGoods()
            }

            LivePlayerIntent.EnterRoom -> {
                enterRoom()
            }

            LivePlayerIntent.FetchLiveRoomNum -> {
                fetchLiveRoomOnLineNum()
            }

            is LivePlayerIntent.FetchLiveTopGood -> {
                if (it.liveId == null || it.liveId == currentLiveItem?.liveId) {
                    fetchLiveRoomTopGood()
                }
            }

            is LivePlayerIntent.FetchLiveRoomDetail -> {
                fetchLiveDetail(it.pullId)
            }
            is  LivePlayerIntent.OtherSettingValue ->{
                getOtherSetting()
            }
        }
    }
    private fun getOtherSetting() {
        request(api = { getOtherSetting("live_image_url") }, success = {
            if (it!=null){
                mState.postEvent(LivePlayerState.OtherSettingValue(it.settingData?:""))
            }

        })
    }

    private fun fetchLiveRoomList() {
        request(api = { liveRoomList() }, success = {
            if (!it.isNullOrEmpty()) {
                currentLiveItem = it[0]
                mState.postEvent(LivePlayerState.HasLiveRoom(currentLiveItem!!))
            } else {
                mState.postEvent(LivePlayerState.NoLiveRoom)
            }
        }, error = {
            postState(LivePlayerState.NoLiveRoom)
            showToast(it.message)
        })
    }

    /**
     * 获取直播间状态
     */
    private fun fetchLiveDetail(pullId: String?) {
        if (pullId == null) return
        request(api = { liveRoomDetail(pullId) }, success = {
            // 判断当前状态 和之前的状态是否一致
            currentLiveItem = it
            if (it != null && it.liveStatus == 1) {
                mState.postEvent(LivePlayerState.HasLiveRoom(currentLiveItem!!))
            } else {
                mState.postEvent(LivePlayerState.NoLiveRoom)
            }
        }, error = {
            postState(LivePlayerState.NoLiveRoom)
            showToast(it.message)
        })
    }

    private fun enterRoom() {
        request(api = { enterLiveRoom(currentLiveItem?.pullId) }, error = {})
    }

    private fun fetchLiveRoomOnLineNum() {
        request(api = { liveRoomOnLineNum(currentLiveItem?.pullId) }, success = {
            mState.postEvent(LivePlayerState.FetchOnlineNumSuccess(it ?: "0"))
        }, error = {})
    }

    private fun exitRoom() {
        request(api = { exitLiveRoom(currentLiveItem?.pullId) }, success = {
            mState.postEvent(LivePlayerState.OnExitRoom)
        }, error = {
            postState(LivePlayerState.OnExitRoom)
        })
    }


    /**
     * 查询直播间列表
     */
    private fun fetchLiveRoomGoods() {
        if (currentLiveItem == null) return
        request(api = { liveRoomGoods(currentLiveItem?.liveId ?: 0) }, success = {
            mState.postEvent(LivePlayerState.FetchGoodSuccess(it))
        })
    }

    /**
     * 查询直播间列表
     */
    private fun fetchLiveRoomTopGood() {
        if (currentLiveItem == null) return
        request(api = { liveRoomTopGood(currentLiveItem?.liveId ?: 0) }, success = {

                good = it
                mState.postEvent(LivePlayerState.FetchTopGoodSuccess(it))

        })
    }
}